package com.example.widgetcase.slice.colorfilter;

import com.example.widgetcase.ResourceTable;
import com.example.widgetcase.slice.BaseSlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Slider;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.ColorMatrix;

public class ColorMatrixAbility extends BaseSlice implements Slider.ValueChangedListener {

    private ColorMatrix matrix;

    private Image jingyIv;
    private Slider rslider;
    private Slider gslider;
    private Slider bslider;
    private Slider aslider;

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_color_matrix;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        jingyIv = (Image) findComponentById(ResourceTable.Id_jingyIv);
        rslider = (Slider) findComponentById(ResourceTable.Id_rSlider);
        gslider = (Slider) findComponentById(ResourceTable.Id_gSlider);
        bslider = (Slider) findComponentById(ResourceTable.Id_bSlider);
        aslider = (Slider) findComponentById(ResourceTable.Id_aSlider);
        rslider.setValueChangedListener(this);
        gslider.setValueChangedListener(this);
        bslider.setValueChangedListener(this);
        aslider.setValueChangedListener(this);

        matrix = new ColorMatrix();
        matrix.setScale(calculate(128), calculate(128), calculate(128), calculate(128));
        PixelMapElement element = new PixelMapElement(jingyIv.getPixelMap());
        element.setColorMatrix(matrix);
        jingyIv.setImageElement(element);
    }

    private float calculate(int progress) {
        return progress / 128f;
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        matrix.setScale(calculate(rslider.getProgress()), calculate(gslider.getProgress()),
                calculate(bslider.getProgress()), calculate(aslider.getProgress()));
        jingyIv.getImageElement().setColorMatrix(matrix);
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {

    }
}
